﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace Ue.Components.VerificationCode
{
    public class VerifyCodeBackgroundComponent : IVerifyCodeComponent
    {
        private Color backgroundColor;
        private VerifyBackgroundImage backgroundImage;
        private IEnumerable<Color> stringColours;

        public VerifyCodeBackgroundComponent(params VerifyBackgroundImage[] backgroundImages)
            : this(new VerifyCodeComponentElement<VerifyBackgroundImage>(backgroundImages)) { }

        public VerifyCodeBackgroundComponent(VerifyCodeComponentElement<VerifyBackgroundImage> backgroundElements)
        {
            this.backgroundImage = backgroundElements.GetRandomElement();
            this.stringColours = this.backgroundImage.Colours;
        }
        public VerifyCodeBackgroundComponent(Color backgroundColor,IEnumerable<Color> stringColours)
        {
            this.backgroundColor = backgroundColor;
            this.stringColours = stringColours;
        }

        public IEnumerable<Color> StringColors
        {
            get { return this.stringColours; }
        }

        #region IVerifyCodeComponent 成员

        public int Priority
        {
            get { return 0; }
        }

        public void Draw(ref VerifyImageDrawContext context)
        {
            if (this.backgroundImage == null)
            {
                context.Graphics.Clear(this.backgroundColor);
            }
            else
            {
                context.Bitmap = new Bitmap(this.backgroundImage.Image, context.Bitmap.Width, context.Bitmap.Height);
            }
        }

        #endregion
    }
}
